<?php
/**
 * 此接口返回的数据用于用户初始化聊天面板，
 * 格式参考 http://www.layui.com/doc/modules/layim.html#init
 *
 * 此接口返回一段json格式的数据，包括当前用户信息、当前用户的好友列表、当前用户加入的群组列表(这里写死的是101)
 *
 * 这里返回的用户信息包含了用户id，这个用户id一定要在bind.php利用Gateway::bindUid绑定，
 * 否则通过Gateway::sendToUid给这个uid发送的数据无法收到
 *
 * 这里面返回的群组信息中包含了群组id，群组id一定要在bind.php利用Gateway::joinGroup加入到群组，
 * 否则该用户无法收到这个群组的消息
 *
 */
use Lib\Db;
require_once __DIR__ . '/__init.php';
_session_start();
$db = Db::instance('laychat');
//===========================================
$se=array();
$avatar_user=config_img(1);
$avatar_custom=config_img(2);
if (!isset($_GET['PHPSESSID'])){
    exit_json(array(
        'code' => 400,
        'msg'  => '请登录'
    ));
}

$cook=$_GET['PHPSESSID'];
//区分前后台
//前--台
if(empty($_GET['uid'])){
    //$cook = str_replace('.','-', $cook);
    $res = $db->query("select * from `users` where id =$cook");
    if($res){
        $se['laychat']=array(
            'id'=>"user-".$cook,
            'username'=>$res[0]['username'],
            'sign'=>'用户',
            'avatar'=>$avatar_user,
        );
    }else{
        //查询临时表user_laychat
        $res_new = $db->query("select * from `user_laychat` where username ='$cook'");
        $se['laychat']=array(
            'id'=>$cook,
            'username'=>$cook,
            'sign'=>'用户',
            'avatar'=>$avatar_user,
        );
    }

    $res_online = $db->query("select * from `users` where id ='$cook'");
    if($res_online){
        //更新users表，用户为在线状态
        Db::instance('laychat')->update('users')->col('online', 'online')->where('id=:id')->bindValue('id', $cook)->limit(1)->query();
    }else{
        //更新user_laychat表，用户为在线状态
        Db::instance('laychat')->update('user_laychat')->col('status', 'online')->where('username=:username')->bindValue('username', $cook)->limit(1)->query();
    }


}else{
    //后--台

    $res_admin = $db->query("select * from `admins` where id ={$cook}");

    if(isset($res_admin[0]['name'])){
        $name = $res_admin[0]['name'];
    }else{
        $name = $res_admin[0]['username'];
    }

    $se['laychat'] = array(
        'id'       => "admin-".$cook,
        'username' => $name,
        'sign'     => '客服',
        'avatar'   => $avatar_custom,
    );
    //更新在线状态
    $db->update('admins')->col('online', 'online')->where('id=:id')->bindValue('id', $cook)->limit(1)->query();

    // 读取好友列表数据
    $friend_arr = $db->query("SELECT `id` FROM `users` where reseller_id={$res_admin[0]['reseller_id']}");
    if($friend_arr){
        foreach($friend_arr as $k=>&$v){
            $res_v=$db->query("SELECT `users`.id, `users`.username, `users`.avatar, `users`.sign, `users`.online, `users`.last_login_address, `users`.last_login_ip FROM `users` where id='".$v['id']."' order by online asc");
            $res_v[0]['id']='user-'.$v['id'];
            $friend_list[$v['id']]=$res_v[0];
        }

        //print_r($friend_list);exit;
        //$db->query("SELECT `friend_uid` FROM `friends` where uid={$res[0]['id']} ");

    }else{
        $friend_list=array();
    }

}




// 当前登录用户的id
$mime_id = $se['laychat']['id'];
// 登录者信息，这个session信息可以在你现有系统登录时设置，格式见login.php
$mime_info = $se['laychat'];
$mime_info['status'] = 'online';
$mime_info = json_encode($mime_info);


$friend_list = $friend_list ? $friend_list : array();
//print_r($frend_list);exit;
// 好友数量
$count = count($friend_list);
foreach ($friend_list as $k=>$v){
    if($v['online']=='offline'){
        $friend_list[$k]['avatar']=IMG_OFFLINE;
        $status_online='离线';
    }else{
        $ClientId_arr = Gateway::getClientIdByUid($v['id']);
        $arr_empty=array();
        foreach ($ClientId_arr as $kk=>$vv){
            if(Gateway::isOnline($vv)){
                $arr_empty[]=$vv;
            }
        }
        if(empty($arr_empty)){
            $friend_list[$k]['avatar']=IMG_OFFLINE;
            $status_online='离线';
        }else{
            $status_online='在线';
        }
    }
    //`users`.id, `users`.username, `users`.avatar, `users`.sign, `users`.online
    $friend_list[$k]['id']=$v['id'];
    $friend_list[$k]['username']=$v['username'];
    $friend_list[$k]['address']=$v['last_login_address'];
    $friend_list[$k]['ip']=$v['last_login_ip'];
    $friend_list[$k]['sign']=$status_online;
}
$field='online';
$friend_list = list_sort_by($friend_list, $field, $sortby = 'desc');
// 好友列表数据
$friend_list = json_encode(array_values($friend_list));
// 获取该用户加入的群组数据
//$group_list = $db->query("SELECT `group`.gid AS id, `group`.groupname, `group`.avatar FROM `group_members` LEFT JOIN `group` on group_members.gid=`group`.gid WHERE uid='$mime_id'");
$group_list = $group_list ? json_encode($group_list) : '[]';

?>
{
"code": 0,
"msg": "",
"data": {
"mine": <?php echo $mime_info;?>
,"friend": [{
"groupname": "会员"
,"id": 1
,"online": <?php echo $count;?>
,"list": <?php echo $friend_list; ?>
}]
,"group": <?php echo $group_list;?>
}
}
